package com.asl.website.serviceNeed.controller;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.asl.website.serviceContent.domain.SysServiceContentDTO;
import com.asl.website.serviceNeed.domain.ServiceDTO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.asl.common.annotation.Log;
import com.asl.common.core.controller.BaseController;
import com.asl.common.core.domain.AjaxResult;
import com.asl.common.enums.BusinessType;
import com.asl.website.serviceNeed.domain.SysServiceNeed;
import com.asl.website.serviceNeed.service.ISysServiceNeedService;
import com.asl.common.utils.poi.ExcelUtil;
import com.asl.common.core.page.TableDataInfo;

/**
 * 需求服务Controller
 * 
 * @author ruoyi
 * @date 2025-05-22
 */
@RestController
@RequestMapping("/serviceNeed/serviceNeed")
public class SysServiceNeedController extends BaseController
{
    @Autowired
    private ISysServiceNeedService sysServiceNeedService;
    /**
     * 获取所有服务列表
     * @param acceptLanguage 语言标识（默认zh-CN）
     * @return 所有服务列表
     */
    @GetMapping("/content/{typeId}")
    public AjaxResult getAllServices(
            @PathVariable("typeId") Integer typeId,
            @RequestHeader(value = "Accept-Language", defaultValue = "zh-CN") String acceptLanguage) {

        SysServiceNeed query = new SysServiceNeed();
        query.setTypeId(typeId);
        // 创建空查询对象，查询所有记录
        List<SysServiceNeed> services = sysServiceNeedService.selectSysServiceNeedList(query);

        if (services.isEmpty()) {
            return AjaxResult.error("未配置任何服务");
        }

        List<ServiceDTO> result = services.stream()
                .map(service -> new ServiceDTO(service, acceptLanguage))
                .sorted(Comparator.comparingInt(ServiceDTO::getSortOrder))
                .collect(Collectors.toList());

        return AjaxResult.success(result.get(0));
    }
    /**
     * 查询需求服务列表
     */
    @PreAuthorize("@ss.hasPermi('serviceNeed:serviceNeed:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysServiceNeed sysServiceNeed)
    {
        startPage();
        List<SysServiceNeed> list = sysServiceNeedService.selectSysServiceNeedList(sysServiceNeed);
        return getDataTable(list);
    }

    /**
     * 导出需求服务列表
     */
    @PreAuthorize("@ss.hasPermi('serviceNeed:serviceNeed:export')")
    @Log(title = "需求服务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysServiceNeed sysServiceNeed)
    {
        List<SysServiceNeed> list = sysServiceNeedService.selectSysServiceNeedList(sysServiceNeed);
        ExcelUtil<SysServiceNeed> util = new ExcelUtil<SysServiceNeed>(SysServiceNeed.class);
        util.exportExcel(response, list, "需求服务数据");
    }

    /**
     * 获取需求服务详细信息
     */
    @PreAuthorize("@ss.hasPermi('serviceNeed:serviceNeed:query')")
    @GetMapping(value = "/{serviceId}")
    public AjaxResult getInfo(@PathVariable("serviceId") Long serviceId)
    {
        return success(sysServiceNeedService.selectSysServiceNeedByServiceId(serviceId));
    }

    /**
     * 新增需求服务
     */
    @PreAuthorize("@ss.hasPermi('serviceNeed:serviceNeed:add')")
    @Log(title = "需求服务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysServiceNeed sysServiceNeed)
    {
        return toAjax(sysServiceNeedService.insertSysServiceNeed(sysServiceNeed));
    }

    /**
     * 修改需求服务
     */
    @PreAuthorize("@ss.hasPermi('serviceNeed:serviceNeed:edit')")
    @Log(title = "需求服务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysServiceNeed sysServiceNeed)
    {
        return toAjax(sysServiceNeedService.updateSysServiceNeed(sysServiceNeed));
    }

    /**
     * 删除需求服务
     */
    @PreAuthorize("@ss.hasPermi('serviceNeed:serviceNeed:remove')")
    @Log(title = "需求服务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{serviceIds}")
    public AjaxResult remove(@PathVariable Long[] serviceIds)
    {
        return toAjax(sysServiceNeedService.deleteSysServiceNeedByServiceIds(serviceIds));
    }
}
